package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.system.dtos.request.AddLevelRuleReq;
import com.rightsidetech.system.dtos.request.UpdateLevelRuleReq;
import com.rightsidetech.system.dtos.response.LevelRuleResp;
import com.rightsidetech.system.dtos.response.LevelSearchResp;
import com.rightsidetech.system.entity.SysLevelRule;
import com.rightsidetech.system.mapper.SysLevelRuleMapper;
import com.rightsidetech.system.service.LevelRuleService;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Package: com.rightsidetech.system.service.impl
 * @ClassName: LevelRuleServiceImpl
 * @Author: yinj
 * @CreateTime: 2021/10/20 10:45
 */
@Service("levelRuleServiceImpl")
public class LevelRuleServiceImpl implements LevelRuleService {

    @Resource
    private SysLevelRuleMapper sysLevelRuleMapper;

    @Override
    public ResponseData<PageInfo<LevelRuleResp>> page(PageRequest request) {
        PageHelper.startPage(request.getPageNo(),request.getPageSize());
        List<LevelRuleResp> list = sysLevelRuleMapper.list();
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData add(AddLevelRuleReq req,Long loginInfoId) {

        SysLevelRule sysLevelRule =  sysLevelRuleMapper.selectByLevel(req.getLevel());
        if (!ObjectUtils.isEmpty(sysLevelRule)){
            return ResponseData.fail("等级已存在");
        }
        sysLevelRule =  sysLevelRuleMapper.selectByLevelName(req.getLevelName());
        if (!ObjectUtils.isEmpty(sysLevelRule)){
            return ResponseData.fail("等级名称已存在");
        }

        SysLevelRule levelRule = new SysLevelRule();
        levelRule.setLevel(req.getLevel());
        levelRule.setLevelName(req.getLevelName());
        levelRule.setOrderNum(req.getOrderNum());
        levelRule.setCreatorId(loginInfoId);
        levelRule.setCreatedAt(new Date());
        sysLevelRuleMapper.insertSelective(levelRule);
        return ResponseData.success();
    }

    @Override
    public ResponseData update(UpdateLevelRuleReq req, Long loginInfoId) {

        SysLevelRule sysLevelRule =  sysLevelRuleMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysLevelRule)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        if (!StringUtils.isEmpty(req.getLevel())){
            sysLevelRule =  sysLevelRuleMapper.selectByLevel(req.getLevel());
            if (!ObjectUtils.isEmpty(sysLevelRule) && !sysLevelRule.getId().equals(req.getId())){
                return ResponseData.fail("等级已存在");
            }
        }

        if (!StringUtils.isEmpty(req.getLevelName())){
            sysLevelRule =  sysLevelRuleMapper.selectByLevelName(req.getLevelName());
            if (!ObjectUtils.isEmpty(sysLevelRule) && !sysLevelRule.getId().equals(req.getId())){
                return ResponseData.fail("等级名称已存在");
            }
        }

        SysLevelRule levelRule = new SysLevelRule();
        levelRule.setLevel(req.getLevel());
        levelRule.setLevelName(req.getLevelName());
        levelRule.setOrderNum(req.getOrderNum());
        levelRule.setCreatorId(loginInfoId);
        levelRule.setCreatedAt(new Date());
        levelRule.setId(req.getId());
        sysLevelRuleMapper.updateByPrimaryKeySelective(levelRule);
        return ResponseData.success();
    }

    @Override
    public ResponseData del(UpdateLevelRuleReq req, Long loginInfoId) {
        SysLevelRule sysLevelRule =  sysLevelRuleMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysLevelRule)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        sysLevelRuleMapper.deleteByPrimaryKey(req.getId());
        return ResponseData.success();
    }

    @Override
    public SysLevelRule findLevelRuleByOrderNum(Long orderNum) {
        return sysLevelRuleMapper.findLevelRuleByOrderNum(orderNum);
    }

    @Override
    public ResponseData<List<LevelSearchResp>> search(Byte level) {
        List<LevelSearchResp> searchResp = sysLevelRuleMapper.search(level);
        return ResponseData.success(searchResp);
    }

    @Override
    public SysLevelRule selectById(Long id) {
        return sysLevelRuleMapper.selectByPrimaryKey(id);
    }
}
